﻿function getParams() {
    var params = {};

    var orderNo = $("input[name='orderNo']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(orderNo)) {
        params.orderNo = orderNo;
    }

    var cardName = $("input[name='cardName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(cardName)) {
        params.cardName = cardName;
    }

    var companyName = $("input[name='companyName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(companyName)) {
        params.companyName = companyName;
    }

    var bankName = $("input[name='bankName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(bankName)) {
        params.bankName = bankName;
    }

    var withdrawalAccountName = $("input[name='withdrawalAccountName']").val();
    if (!FlashPay.Util.isNullOrEmptySpance(withdrawalAccountName)) {
        params.withdrawalAccountName = withdrawalAccountName;
    }

    var noticeStatus = $("select[name='noticeStatus']").find("option:selected").val();
    if (!FlashPay.Util.isNullOrEmptySpance(noticeStatus)) {
        params.noticeStatus = noticeStatus;
    }

    params.startTime = $("input[name='startTime']").val();
    params.endTime = $("input[name='endTime']").val();

    return params;
}

function replace_field(key) {

    var data = {
        "订单号": "Order#",
        "公司名称": "Company Name",
        "收款单号": "Receipt#",
        "收款银行": "Receipt Bank",
        "收款姓名": "Receipt Name",
        "收款卡号": "Receipt Card Number",
        "收款金额": "Receipt Amount",
        "收款时间": "Receipt Time",
        "交易类型": "Transaction Type",
        "通知状态": "Notification Status",
        "通知次数": "Notification Number",
        "客户姓名": "Customer Came",
        "客户银行": "Customer Bank",
        "客户卡号": "Customer Card#",
        "客户附言": "Postscript",
        "创建时间": "Creation Time",
        "匹配流水号": "Match Id",
    };

    var language = FlashPay.Util.Language();

    if (!FlashPay.Util.isNullOrEmptySpance(language) && language == "en") {
        return data[key];
    } else {
        return key;
    }
}

function doSearch() {

    var frozenColumns = [[
        { field: 'orderNo', title: replace_field('订单号'), width: 130, align: 'left' },
        {
            field: 'companyName',
            title: replace_field('公司名称'),
            width: 100,
            align: 'left',
            formatter: function (val, row) {
                var result = "<span class='bold'>{0}</span>".format(val == null ? "" : val);
                if (row.noticeStatus == 3 && row.noticeTimes == 5 && row.changeCompanyPermission == "ReceiptRecord0005") {
                    result += ' <a class="btn btn-warning btn-xs" data-modal="/view/receipt/receiptrecordchangecompany.html?orderNo={0}&originalCompanyId={1}" data-title="{0}-改变公司" data-confirmname="确认" data-width="300" data-height="100">改变公司</a>'.format(row.orderNo, row.companyId);
                }
                return result;
            }
        },
        { field: 'withdrawalOrderNo', title: replace_field('收款单号'), width: 180, align: 'left' },
        { field: 'bankName', title: replace_field('收款银行'), width: 65, align: 'left' },
        { field: 'cardName', title: replace_field('收款姓名'), width: 65, align: 'left' },
        { field: 'cardNumber', title: replace_field('收款卡号'), width: 120, align: 'left' },
        {
            field: 'createDbdate',
            title: replace_field('创建时间'),
            width: 120,
            align: 'left',
            formatter: function (val, row) {
                return FlashPay.Util.FormatDate(val);
            }
        },
        {
            field: 'withdrawalAmount',
            title: replace_field('收款金额'),
            width: 65,
            align: 'right',
            formatter: function (val, row) {
                return '<span class="bold">{0}</span>'.format(val);
            }
        }
    ]];

    var columns = [[
        {
            field: 'depositDate',
            title: replace_field('收款时间'),
            width: 120,
            align: 'left',
            formatter: function (val, row) {
                return FlashPay.Util.FormatDate(val);
            }
        },
        {
            field: 'depositType',
            title: replace_field('交易类型'),
            width: 100,
            align: 'left',
            formatter: function (val, row) {
                return val;
            }
        },
        {
            field: 'noticeStatus',
            title: replace_field('通知状态'),
            width: 125,
            align: 'left',
            formatter: function (val, row) {
                var returnStr = "";
                if (val == 1) {
                    returnStr = "<span class='bold'>未通知</span>";
                    if (row.timeDifference > 6) {
                        returnStr += ' <a class="btn btn-warning btn-xs" data-confirm="{0}<br>确定要<span class=orange>重置</span>吗？" data-href="/Receipt/ResetNoticeStatus?orderNo={0}">重置</a>'.format(row.orderNo);
                    }
                } else if (val == 2) {
                    returnStr = '<span class="green bold">通知成功</span>';
                } else if (val == 3) {
                    returnStr = "<span class='red bold' title='{0}'>通知失败</span>".format(row.depositRemark);
                    if (row.timeDifference > 6 && row.noticeTimes >= 5) {
                        returnStr += ' <a class="btn btn-warning btn-xs" data-confirm="{0}<br>确定要<span class=orange>重置</span>吗？" data-href="/Receipt/ResetNoticeStatus?orderNo={0}">重置</a>'.format(row.orderNo);
                    }
                } else if (val == 4) {
                    returnStr = "<span class='orange bold'>通知中</span>";
                } else if (val == 5) {
                    returnStr = "<span class='bold' title='请到\"收款匹配记录\"查询匹配，补推送'>不通知</span>";
                    if (row.makeUpPermission == "ReceiptRecord0003" && row.timeDifference < 2880) {
                        returnStr += ' <a class="btn btn-warning btn-xs" data-confirm="{0}<br>确定要<span class=orange>重置</span>吗？" data-href="/Receipt/MakeUp?orderNo={0}">重置</a>'.format(row.orderNo);
                    }
                }

                return returnStr;
            }
        },
        {
            field: 'noticeLastDate',
            title: replace_field('通知时间'),
            width: 120,
            align: 'left',
            formatter: function (val, row) {
                return FlashPay.Util.FormatDate(val);
            }
        },
        { field: 'noticeTimes', title: replace_field('通知次数'), width: 75, align: 'left' },
        { field: 'withdrawalAccountName', title: replace_field('客户姓名'), width: 75, align: 'left' },
        { field: 'withdrawalBankName', title: replace_field('客户银行'), width: 150, align: 'left' },
        { field: 'withdrawalCardNumber', title: replace_field('客户卡号'), width: 200, align: 'left' },
        { field: 'depositMatchId', title: replace_field('匹配流水号'), width: 150, align: 'left' },
        { field: 'postScript', title: replace_field('客户附言'), width: 200, align: 'left' }
    ]];

    $("#receiptRecord").datagrid({
        fit: false,
        loadMsg: '数据加载中，请稍后……',
        method: "get",
        border: true,
        url: '/Receipt/GetReceiptRecords',
        queryParams: getParams(),
        pagination: true,
        pageSize: pageSize,
        pageList: [15, 30, 50, 100, 1000],
        height: $(window).height() - 70,
        singleSelect: true,
        rownumbers: true,
        showFooter: true,
        idField: 'orderNo',
        onLoadSuccess: function () {
            pageSize = $('#receiptRecord').datagrid('options').pageSize;
        },
        frozenColumns: frozenColumns,
        columns: columns
    });
}